package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.SellerRatingsDao;
import com.ngm.tea.tea.entity.SellerRatings;
import com.ngm.tea.tea.service.SellerRatingsService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("sellerRatingsService")
public class SellerRatingsServiceImpl implements SellerRatingsService {
	
	@Resource
	private SellerRatingsDao sellerRatingsDao;

	public SellerRatingsDao getSellerRatingsDao() {
		return sellerRatingsDao;
	}

	@Override
	public void insert(SellerRatings sellerRatings) {
		sellerRatingsDao.insert(sellerRatings);
	}

	@Override
	public void deleteById(Long id) {
		sellerRatingsDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			sellerRatingsDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(SellerRatings sellerRatings) {
		sellerRatingsDao.delete(sellerRatings);
	}

	@Override
	public void update(SellerRatings sellerRatings) {
		sellerRatingsDao.update(sellerRatings);
	}

	@Override
	public SellerRatings selectOne(Long id) {
		return sellerRatingsDao.selectOne(id);
	}

	@Override
	public List<SellerRatings> selectAll() {
		return sellerRatingsDao.selectAll();
	}

	@Override
	public List<SellerRatings> selectList(SellerRatings sellerRatings, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(sellerRatings));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return sellerRatingsDao.selectList(map);
	}

	@Override
	public Long count(SellerRatings sellerRatings) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(sellerRatings));
		return sellerRatingsDao.count(map);
	}
	
	public String makeStr(SellerRatings sellerRatings) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(sellerRatings.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + sellerRatings.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + sellerRatings.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getUsername())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("username LIKE '%" + sellerRatings.getUsername()+"%'");
			} else {
				sb.append(" AND username LIKE '%" + sellerRatings.getUsername()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getRateTime())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rateTime LIKE '%" + sellerRatings.getRateTime()+"%'");
			} else {
				sb.append(" AND rateTime LIKE '%" + sellerRatings.getRateTime()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getDeliveryTime())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("deliveryTime LIKE '%" + sellerRatings.getDeliveryTime()+"%'");
			} else {
				sb.append(" AND deliveryTime LIKE '%" + sellerRatings.getDeliveryTime()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getScore())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("score LIKE '%" + sellerRatings.getScore()+"%'");
			} else {
				sb.append(" AND score LIKE '%" + sellerRatings.getScore()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getRateType())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rateType LIKE '%" + sellerRatings.getRateType()+"%'");
			} else {
				sb.append(" AND rateType LIKE '%" + sellerRatings.getRateType()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getText())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("text LIKE '%" + sellerRatings.getText()+"%'");
			} else {
				sb.append(" AND text LIKE '%" + sellerRatings.getText()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getAvatar())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("avatar LIKE '%" + sellerRatings.getAvatar()+"%'");
			} else {
				sb.append(" AND avatar LIKE '%" + sellerRatings.getAvatar()+"%'");
			}
		}
		if(!StringUtils.isEmpty(sellerRatings.getRecommend())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("recommend LIKE '%" + sellerRatings.getRecommend()+"%'");
			} else {
				sb.append(" AND recommend LIKE '%" + sellerRatings.getRecommend()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}
	
}